.SUFFIXES: 

ARDUINODIR=c:/Arduino/

ARDUINO_CORE = $(ARDUINODIR)hardware/arduino/cores/arduino

ARDUINO_LIB = $(ARDUINODIR)libraries

CC=$(ARDUINODIR)hardware/tools/avr/bin/avr-g++
AR=$(ARDUINODIR)hardware/tools/avr/bin/avr-ar
OBJCPY=$(ARDUINODIR)hardware/tools/avr/bin/avr-objcopy
DUDECONF=$(ARDUINODIR)hardware/tools/avr/etc/avrdude.conf

INCLUDEDIRS = "$(ARDUINODIR)hardware/arduino/cores/arduino/" \
"$(ARDUINODIR)hardware/arduino/variants/standard/" \
"$(ARDUINODIR)hardware/tools/avr/include/" \
"$(ARDUINODIR)Libraries/Wire/" \
"$(ARDUINODIR)Libraries/Wire/utility" \
"$(ARDUINODIR)Libraries/" \
"$(ARDUINODIR)libraries/LiquidCrystal"

LIBDIR=$(ARDUINODIR)/Libraries

DEFINES  = F_CPU=16000000L \
USB_VID=null \
USB_PID=null \
ARDUINO=105


SOURCEDIR=

SOURCEFILES = \
$(SOURCEDIR)main.cpp 
 

CORESOURCEFILES_C= \
avr-libc/malloc.c \
avr-libc/realloc.c \
wiring_pulse.c \
wiring_analog.c \
wiring.c \
wiring_digital.c \
WInterrupts.c \
wiring_shift.c 

CORESOURCEFILES_CPP=CDC.cpp \
HardwareSerial.cpp \
HID.cpp \
IPAddress.cpp \
main.cpp \
new.cpp \
Print.cpp \
Stream.cpp \
Tone.cpp \
USBCore.cpp \
WMath.cpp \
WString.cpp 


CORESOURCEFILES = $(CORESOURCEFILES_C) $(CORESOURCEFILES_CPP)

CORELIBSOURCE= (addprefix $(ARDUINO_CORE)/, $(CORESOURCEFILES)) $(ARDUINO_LIB)/EEPROM/EEPROM.cpp 

CORESOURCEFILES += EEPROM.cpp
#$(ARDUINO_LIB)/EEPROM/EEPROM.cpp \

OUTPUT_DIR = build/

COREOBJ =  $(addprefix $(OUTPUT_DIR), $(CORESOURCEFILES_C:.c=.o)) 
COREOBJ += $(addprefix $(OUTPUT_DIR), $(CORESOURCEFILES_CPP:.cpp=.o))

$(info "COREOBJ $(COREOBJ)\n\n")



INCLUDES = $(addprefix -I, $(INCLUDEDIRS))

DEFINES  = F_CPU=16000000L \
USB_VID=null \
USB_PID=null \
ARDUINO=105


DEFINEDVARS = $(addprefix -D, $(DEFINES))

CFLAGS = -Os -ffunction-sections -fdata-sections  
CFLAGS += $(INCLUDES) -mmcu=atmega328p -fno-exceptions -Os $(DEFINEDVARS)

NEWCFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections \
 -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105



#LDFLAGS = -Wl,--gc-sections 
LDFLAGS = -Os -Wl,--gc-sections -mmcu=atmega328p -lm -Lbuild/

OBJECTS =  $(SOURCEFILES:.cpp=.o)

TARGETOBJ = $(addprefix $(OUTPUT_DIR), $(OBJECTS))

O_FILES := $(wildcard libs/*.o)

EXTOBJECTS  = libs/Wire/Wire.cpp.o
EXTOBJECTS += libs/Wire/utility/twi.c.o
EXTOBJECTS += $(ARDUINODIR)hardware/tools/avr/avr/lib/avr5/libm.a

CORELIB=$(OUTPUT_DIR)libcore.a
LIB=$(OUTPUT_DIR)lib.a
#  avr-objcopy -j .text -j .data -O ihex file_name.elf file_name.hex
# avrdude -p <microcontroller> -P /dev/ttyACM0 -c stk500v2 -u -U flash:w:filename.hex

EXECUTABLE=$(OUTPUT_DIR)arduinko.elf
$(info "target objects: $(TARGETOBJ)")

HEX = $(EXECUTABLE:.elf=.hex)

.PHONY : all clean
all: $(EXECUTABLE) STRIP FLASH

clean :
	rm $(OUTPUT_DIR)*

STRIP : $(EXECUTABLE)
	$(OBJCPY)   -R .eeprom -R .flash -O ihex $(EXECUTABLE) $(HEX) 
	avr-size $(HEX)
	avr-objdump -a $(HEX)

FLASH : STRIP
	avrdude -C $(DUDECONF) -F -p atmega328p -PCOM6 -b57600 -carduino  -u -U $(HEX)

$(EXECUTABLE): $(TARGETOBJ) $(CORELIB) $(LIB)
	$(info "Command: $(CC) $? $(EXTOBJECTS) $(CFLAGS) -lm -l$(CORELIB) $(LDFLAGS) -o $@ ")
	$(CC) $? $(EXTOBJECTS) $(CFLAGS) -lm $(CORELIB)  $(LDFLAGS) -o $@

$(CORELIB): $(COREOBJ)
	@for i in $(COREOBJ); do echo $(AR) rcs output/core.a $$i; $(AR) rcs $(CORELIB) $$i; done

LIBSOURCE = $(SOURCEDIR)SFE_BMP180.cpp \
LiquidCrystal.cpp \
LCD.cpp \
LiquidCrystal_I2C.cpp \
I2CIO.cpp \
FastIO.cpp 

LIBO =  $(LIBSOURCE:.cpp=.o)
LIBOBJ = $(addprefix $(OUTPUT_DIR), $(LIBO))

$(info "Command: $(LIBOBJ)")

$(LIB) : $(LIBOBJ)
	@for i in $(LIBOBJ); do echo $(AR) rcs output/liba.a $$i; $(AR) rcs $(LIB) $$i; done



$(OUTPUT_DIR)%.o : $(ARDUINODIR)libraries/LiquidCrystal/%.cpp
	$(CC) $< $(CFLAGS) -c -o $@ -v
	
$(OUTPUT_DIR)%.o : $(SOURCEDIR)%.cpp
	$(CC) $< $(CFLAGS) -c -o $@ -v

$(OUTPUT_DIR)%.o : $(ARDUINO_CORE)/%.cpp
	$(CC) $< $(CFLAGS) -c -o $@ 

$(OUTPUT_DIR)%.o : $(SOURCEDIR)%.c
	$(CC) $< $(CFLAGS) -c -o $@

$(OUTPUT_DIR)%.o : $(ARDUINO_CORE)/%.c
	$(CC) $< $(CFLAGS) -c -o $@
	
